/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiface.promonet.servlet.log;

import java.util.Date;
import java.util.LinkedList;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import multiface.promonet.jpa.Logdb;
import multiface.promonet.jpa.Utente;
import multiface.promonet.jpa.util.PersistenceManager;
import multiface.web.util.Define;
import multiface.web.util.Util;

/**
 *
 * @author Diego64
 */
public class ActionLogDb {

    public static final String[] excludeParameters = {"psw", "password","action"};

    
    
    public static void addLog(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        HttpSession s = request.getSession();
        if (s.getAttribute(Define.USER_SESSION) != null) {

            Utente u_session = (Utente) s.getAttribute(Define.USER_SESSION);

            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            Logdb l = new Logdb();

            l.setUserAgent(request.getHeader("user-agent"));
            l.setDataora(new Date());
            l.setParametri(ActionLogDb.mapToString(request.getParameterMap()));
            l.setSessionid(s.getId());
            l.setUserinfo(u_session.getNome() + " " + u_session.getCognome() + " " + u_session.getEmail());
            l.setAction(request.getParameter("action"));
            l.setCommento("Error: " + Util.StampaStackError(ex));

            em.getTransaction().begin();
            em.persist(l);
            em.getTransaction().commit();

            em.close();
        }
    }
    public static void addLog(HttpServletRequest request, HttpServletResponse response) {
        HttpSession s = request.getSession();
        if (s.getAttribute(Define.USER_SESSION) != null) {

            Utente u_session = (Utente) s.getAttribute(Define.USER_SESSION);

            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            Logdb l = new Logdb();

            l.setUserAgent(request.getHeader("user-agent"));
            l.setDataora(new Date());
            l.setParametri(ActionLogDb.mapToString(request.getParameterMap()));
            l.setSessionid(s.getId());
            l.setUserinfo(u_session.getNome() + " " + u_session.getCognome() + " " + u_session.getEmail());
            l.setAction(request.getParameter("action"));
            l.setCommento("OK");

            em.getTransaction().begin();
            em.persist(l);
            em.getTransaction().commit();

            em.close();
        }
    }

    public static String mapToString(Map<String, String[]> map) {
        LinkedList<String> list = new LinkedList<String>();

        for (Object key : map.keySet()) {
            String keyStr = (String) key;
            if (!excludeParams(keyStr)) {
                String[] value = map.get(keyStr);
                list.add("[" + key + ":" + value[0] + "]");
            }
        }

        String s = "";
        int c = 0;
        for (String l : list) {
            s += l;
            c++;
            if (c < list.size()) {
                s += ",";
            }
        }
        return s;

    }

    public static boolean excludeParams(String key) {
        boolean b = false;
        for (int i = 0; i < excludeParameters.length; i++) {
            if (key.contains(excludeParameters[i])) {
                b = true;
            }
        }
        return b;
    }
}
